angular.module('Controller')

.controller('QgyContractEditCtrl', ($scope,NgTableParams,qgyApiService,menuManage,wfAlert,$localStorage,commonUtil,commonConstant,$rootScope,$uibModal,$http) ->
    if $rootScope.pageType=='contract-edit'
#接受数据:{houseId,contractId,house,contract}
        data=angular.copy($rootScope.pageParams)
        $scope.contractId=data.contractId
        $rootScope.pageParams=undefined
        $rootScope.pageType=undefined
    $scope.housePayTypeList=[
        {value: 1, name: '押一付三'},
        {value: 2, name: '押一付一'},
        {value: 3, name: '押一付一/押一付三'},
        {value: 4, name: '租赁贷'}
        {value: 5, name: '押一付三/租赁贷'}
        {value: 6, name: '押一付一/租赁贷'}
        {value: 7, name: '押一付三/押一付一/租赁贷'}
    ]
    loadData=()->
        loadContractById($scope.contractId)
    loadContractById=(contractId)->
        $http.get("/contract/#{contractId}/detail")
        .success((resp)->
            if resp.code==0
                $scope.contract=resp.message
                $scope.contract.cotenants=[] if $scope.contract.cotenants==undefined
                $scope.oriContract=angular.copy($scope.contract)
        )
    loadData()
    $scope.sexList=commonConstant.numSexList()
    $scope.uploadImageData=(eleId,param)->
        qgyApiService.bindImageUpload(eleId,param,(param,data)->
            $scope.contract[param]=data
        )
        $(eleId).click()
        ''
    _.extend $scope,
        openCotenant:(type,item)->
            modal=showCotenantModal(type,item)
            modal.result.then((modalData)->
                if modalData && type!='view'
                    $scope.contract.cotenants=modalData
            )
            ''
        resetData:()->
            $scope.contract=angular.copy($scope.oriContract)
        saveData:()->
            reqData={
                "customerCard":$scope.contract.customerCard
                "customerName":$scope.contract.customerName
                "sex":$scope.contract.sex
                "idcardFront":$scope.contract.idcardFront
                "idcardBack":$scope.contract.idcardBack
                "cotenant":$scope.contract.cotenants
            }
            $http.put("/contract/#{$scope.contractId}",reqData)
            .success((resp)->
                if resp.code==0
                    loadContractById($scope.contractId)
                    $rootScope.$broadcast('refresh',{contractId:$scope.contractId})
                    wfAlert.toastInfo('合同编辑成功')
                else
                    wfAlert.toastError(resp.message)
            )
        printContract:()->
            cnt=$scope.contract
            if cnt and cnt.contractPdf
                window.open(cnt.contractPdf, '_blank')
            else
                wfAlert.toastError('合同pdf文件未生成')
            ''
        fetchUser:()->
            return
            if $scope.person.customerPhone?.length==11
                pm=qgyApiService.fetchAsyncUser($scope.person.customerPhone)
                pm.then((personData)->
                    $scope.contract.customerName=personData.customerName
                    $scope.contract.customerCard=personData.customerCard
                    $scope.contract.customerPhone=personData.customerPhone
                    $scope.contract.sex=personData.sex
                    $scope.contract.idcardFront=personData.idcardFront
                    $scope.contract.idcardBack=personData.idcardBack
                )
    showCotenantModal=(type,data)->
        modal = $uibModal.open({
            animation: true,
            size:'lg',
            appendTo: $("#modalContent:eq(0)"),
            controller: 'QgyCotenantModalCtrl',
            templateUrl: 'templates/business-manage/cotenant-modal/cotenant-modal.html',
            resolve: {
                items:  ()->{type:type,data:data}
            }
        })
)